/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import dao.UserDAO;
import entities.User;
import game.Table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import org.icefaces.application.PushRenderer;
import other.Costants;
import other.TextModel;



/**
 *
 * @author alessandro
 */
@ManagedBean
@ViewScoped
public class ChatBean implements Serializable {

    @ManagedProperty(value = "#{messageBean}")
    private MessageBean messageBean;
    @ManagedProperty(value = "#{userManager}")
    private UserManager userManager;
    @ManagedProperty(value = "#{chatUserBean}")
    private ChatUserBean chatUserBean;
    @ManagedProperty(value = "#{tablesManager}")
    private TablesManager tablesManager;

    private String message = "";
    private String user = "";
    private List<TextModel> textList;
    private List<String> userList;
    private int bots;
    private List<Table> tableList;
    private List<User> ranking;

    /** Creates a new instance of ChatBean */
    public ChatBean() {
        PushRenderer.addCurrentView(Costants.PUSH_GROUP);
        //chatUser.addToList(user);
        /*FacesContext fcontext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fcontext.getExternalContext().getSession(false);
        sessionId = session.getId();*/
    }

    @PostConstruct
    public void init() {
        user = userManager.getUser().getUser();
        chatUserBean.addToList(user);
    }

    public int getBots() {
        return bots;
    }

    public void setBots(int bots) {
        this.bots = bots;
    }

    public TablesManager getTablesManager() {
        return tablesManager;
    }

    public void setTablesManager(TablesManager tablesManager) {
        this.tablesManager = tablesManager;
    }

    public List<User> getRanking() {
        ranking = UserDAO.getRank();
        return ranking;
    }

    public void setRanking(ArrayList<User> ranking) {
        this.ranking = ranking;
    }
    
        public List<Table> getTableList() {
        tableList = tablesManager.getTableList();
        return tableList;
    }

    public void setTableList(List<Table> tableList) {
        this.tableList = tableList;
    }

    public List<String> getUserList() {
        userList = chatUserBean.getUserList();
        return userList;
    }

    public void setUserList(List<String> userlist) {
        this.userList = userlist;
    }

    public List<TextModel> getTextList() {
        textList = messageBean.getTextList();
        return textList;
    }

    public void setTextList(List<TextModel> textList) {
        this.textList = textList;
    }




    public ChatUserBean getChatUserBean() {
        return chatUserBean;
    }

    public void setChatUserBean(ChatUserBean chatUserBean) {
        this.chatUserBean = chatUserBean;
    }

    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public UserManager getUserManager() {
        return userManager;
    }
    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public MessageBean getMessageBean() {
        return messageBean;
    }
    public void setMessageBean(MessageBean messageBean) {
        this.messageBean = messageBean;
    }

    public String sendMessage() {
        user = userManager.getUser().getUser();
        messageBean.addToList(user, message);
        message = "";
        PushRenderer.render(Costants.PUSH_GROUP);
        return null;
    }

    public void addToList() {
        user = userManager.getUser().getUser();
        chatUserBean.addToList(user);
        PushRenderer.render(Costants.PUSH_GROUP);
    }

    public void createTable() {
        PushRenderer.removeCurrentView(Costants.PUSH_GROUP);
        String username = userManager.getUsername();
        tablesManager.addTable(username, bots, username);
        tablesManager.addWatcher(username, username);
        messageBean.addTable(username);
        userManager.setState(Costants.WATCHING);
    }
}
